<?php
/**
 * @package App
 * @subpackage resource
 */
 
/**
 *
 * Yahoo API リソース
 * 
 * http://developer.yahoo.com/
 *
 * @package     App
 * @subpackage  resource
 * @author      $Author: moriwaki $
 * @version     $Id: yahoo.php 360 2008-10-1 01:58:40Z moriwaki $
 * 
 */
class Yahoo extends BEAR_Vo
{
 
    /**
     * API uri
     */
    private static $uri = "http://boss.yahooapis.com/ysearch";

	/**
	 * yahoo api key
	 */
	private static $apiKey = '';
	
    /**
     * 作成
     * 
     * @param array $values
     * + name: string 名前
     * @return integer id or error
     */
    public function onCreate($values)
    {
    }
 
    /**
     * 変更
     * 
     * @param array $values
     * + id: integer $id
     * + name: string 名前
     * @return integer id or error
     */
    public function onUpdate($values)
    {
    }
 
    /**
     * 読み込み
     * 
     * @param array $values
     * + mode: strign $mode
     * @return array
     */
    public function onRead($values)
    {
        //assert & mapping
        self::assert(isset($values['mode']));
        $mode = $values['mode'];
	    $option = array(
	    "timeout" => "3", // タイムアウトの秒数指定
	    );
	
        switch($mode){
	    case 'boss_web_list' :

	        $query = App_Values::getValue('query', $values);
	        $query = str_replace(' ', '%20', $query);

	        $url = self::$uri."/web/v1/{$query}";
	        $params = array();
	        $params['appid'] = self::$apiKey;
	        $params['start'] = App_Values::getValue('start', $values, 0);
	        $params['count'] = App_Values::getValue('count', $values, 10);
	        $params['lang'] = 'jp';
	//        $params['filter'] = '-porn';
	        $params['format'] = 'xml';
	        $params['region'] = 'jp';

	        $p = new App_Url($url, $params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        //$req->setProxy(PROXY_SERVER,PROXY_PORT);
	        $response = $req->sendRequest();

	        if (!PEAR::isError($response)) { 
	            $result = $req->getResponseBody();

	            $parser = new XML_Unserializer(array('parseAttributes' => true));
	            $parser->unserialize($result);
	            $result = $parser->getUnserializedData();

	            if (isset($result['resultset_web']['result']['clickurl'])){
	                $tmp = $result['resultset_web']['result'];

	                $result['resultset_web']['result'] = array();
	                $result['resultset_web']['result'][] = $tmp;
	            }
	            $result['timeout'] = 0;
	        }else{
	            $result['timeout'] = 1;
	        }
	        break;

	    case 'boss_images_list' :

	        $query = getValue($values, 'query');
	        $query = str_replace(' ', '%20', $query);

	        $url = self::$uri."/images/v1/{$query}";

	        $params = array();
	        $params['appid'] = self::$apiKey;
	        $params['start'] = App_Values::getValue('start', $values, 0);
	        $params['count'] = App_Values::getValue('count', $values, 10);
	        $params['lang'] = 'jp';
	        $params['filter'] = 'yes';
	        $params['format'] = 'xml';
	        $params['region'] = 'jp';

	        $p = new App_Url($url,$params);
	        $url = $p->getUrl();

	        $req=& new HTTP_Request($url, $option);
	        //$req->setProxy(PROXY_SERVER,PROXY_PORT);
	        $response = $req->sendRequest();
	        if (!PEAR::isError($response)) { 
	            $result = $req->getResponseBody();
	            $parser = new XML_Unserializer(array('parseAttributes' => true));
	            $parser->unserialize($result);
	            $result = $parser->getUnserializedData();

	            if (isset($result['resultset_images']['result']['clickurl'])){
	                $tmp = $result['resultset_images']['result'];

	                $result['resultset_images']['result'] = array();
	                $result['resultset_images']['result'][] = $tmp;
	            }
	            $result['timeout'] = 0;
	        }else{
	            $result['timeout'] = 1;
	        }
	        break;
        }

        return $result;
    }
 
    /**
     * 削除
     * 
     * @param array $values
     * + id: integer id
     * @return MDB2_Result
     */
    public function onDelete($values)
    {
    }
}
